package com.yanq.liancar.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanq.liancar.R;
import com.yanq.liancar.bean.CarBrand;
import com.yanq.liancar.bean.CarPinpaiBean;
import com.yanq.liancar.util.ContactComparator;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;

public class CarBrandAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    //private String[] mCarBrandNames; // 联系人名称字符串数组
    private List<String> mCarBrandList; // 联系人名称List（转换成拼音）
    private List<CarBrand> resultList; // 最终结果（包含分组的字母）
    private List<String> characterList; // 字母List
    private OnItemClick onItemClick; //
    private int pos; //
    private Integer[] colors = {R.drawable.dian_green, R.drawable.dian_red, R.drawable.dian_blue, R.drawable.dian_yellow, R.drawable.dian_zong};

    private List<CarPinpaiBean.DataBean> mPinpaiList;  //数据源


    public enum ITEM_TYPE {
        ITEM_TYPE_CARBRAND,
        ITEM_TYPE_CONTACT
    }

    //    public CarBrandAdapter(Context context, String[] carBrandtNames) {
    //        mContext = context;
    //        mLayoutInflater = LayoutInflater.from(context);
    //        mCarBrandNames = carBrandtNames;
    //
    //        handleCarBrand();
    //    }


    public CarBrandAdapter(Context context, List<CarPinpaiBean.DataBean> mList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mPinpaiList = mList;
        LogUtils.d("mPinpaiList.size():", mPinpaiList.size() + "");
        LogUtils.d("mPinpaiList.size():", mPinpaiList.size() + "");
        LogUtils.d("mPinpaiList.size():", mPinpaiList.size() + "");
        handleCarBrand();
    }

    //    private void handleCarBrand() {
    //        mCarBrandList = new ArrayList<>();
    //        Map<String, String> map = new HashMap<>();
    //
    //        for (int i = 0; i < mCarBrandNames.length; i++) {
    //            String pinyin = Utils.getPingYin(mCarBrandNames[i]);
    //            map.put(pinyin, mCarBrandNames[i]);
    //            mCarBrandList.add(pinyin);
    //        }
    //        Collections.sort(mCarBrandList, new ContactComparator());
    //
    //        resultList = new ArrayList<>();
    //        characterList = new ArrayList<>();
    //
    //        for (int i = 0; i < mCarBrandList.size(); i++) {
    //            String name = mCarBrandList.get(i);
    //            String character = (name.charAt(0) + "").toUpperCase(Locale.ENGLISH);
    //            if (!characterList.contains(character)) {
    //                if (character.hashCode() >= "A".hashCode() && character.hashCode() <= "Z".hashCode()) { // 是字母
    //                    characterList.add(character);
    //                    resultList.add(new CarBrand(character, ITEM_TYPE.ITEM_TYPE_CARBRAND.ordinal()));
    //                } else {
    //                    if (!characterList.contains("#")) {
    //                        characterList.add("#");
    //                        resultList.add(new CarBrand("#", ITEM_TYPE.ITEM_TYPE_CARBRAND.ordinal()));
    //                    }
    //                }
    //            }
    //            resultList.add(new CarBrand(map.get(name), ITEM_TYPE.ITEM_TYPE_CONTACT.ordinal()));
    //        }
    //    }

    private void handleCarBrand() {
        mCarBrandList = new ArrayList<>();
        Map<String, String> map = new HashMap<>();

        for (int i = 0; i < mPinpaiList.size(); i++) {
            LogUtils.d("pinyin:", "111111111111");
            String pinyin = Utils.getPingYin(mPinpaiList.get(i).getCar_trademark());
            LogUtils.d("pinyin:", pinyin);
            map.put(pinyin, mPinpaiList.get(i).getCar_trademark());
            mCarBrandList.add(pinyin);
        }
        Collections.sort(mCarBrandList, new ContactComparator());

        resultList = new ArrayList<>();
        characterList = new ArrayList<>();

        for (int i = 0; i < mCarBrandList.size(); i++) {
            String name = mCarBrandList.get(i);
            String character = (name.charAt(0) + "").toUpperCase(Locale.ENGLISH);
            if (!characterList.contains(character)) {
                if (character.hashCode() >= "A".hashCode() && character.hashCode() <= "Z".hashCode()) { // 是字母
                    characterList.add(character);
                    resultList.add(new CarBrand(character, ITEM_TYPE.ITEM_TYPE_CARBRAND.ordinal()));
                } else {
                    if (!characterList.contains("#")) {
                        characterList.add("#");
                        resultList.add(new CarBrand("#", ITEM_TYPE.ITEM_TYPE_CARBRAND.ordinal()));
                    }
                }
            }
            resultList.add(new CarBrand(map.get(name), ITEM_TYPE.ITEM_TYPE_CONTACT.ordinal()));
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE.ITEM_TYPE_CARBRAND.ordinal()) {
            return new CharacterHolder(mLayoutInflater.inflate(R.layout.item_character, parent, false));
        } else {
            return new CarBrandHolder(mLayoutInflater.inflate(R.layout.item_carbrand, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof CharacterHolder) {
            ((CharacterHolder) holder).mTextView.setText(resultList.get(position).getmName());
        } else if (holder instanceof CarBrandHolder) {
            Random random = new Random();
            ((CarBrandHolder) holder).mTextView.setText(resultList.get(position).getmName());
            ((CarBrandHolder) holder).start.setText(resultList.get(position).getmName().substring(0, 1));
            ((CarBrandHolder) holder).start.setBackground(mContext.getDrawable(colors[random.nextInt(5)]));
            ((CarBrandHolder) holder).item_carBrand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //                    Toast.makeText(mContext, ""+resultList.get(position).getmName(), Toast.LENGTH_SHORT).show();
                    onItemClick.OnItemClick(resultList.get(position).getmName(), position);
                }
            });
        }
    }

    public void OnItemClick(OnItemClick onItemClick,int pos) {
        this.onItemClick = onItemClick;
        this.pos=pos;
    }

    public interface OnItemClick {
        void OnItemClick(String f, int pos);
    }

    @Override
    public int getItemViewType(int position) {
        return resultList.get(position).getmType();
    }

    @Override
    public int getItemCount() {
        return resultList == null ? 0 : resultList.size();
    }

    public class CharacterHolder extends RecyclerView.ViewHolder {
        TextView mTextView;

        CharacterHolder(View view) {
            super(view);

            mTextView = (TextView) view.findViewById(R.id.character);
        }
    }

    public class CarBrandHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        TextView start;
        LinearLayout item_carBrand;

        CarBrandHolder(View view) {
            super(view);

            mTextView = (TextView) view.findViewById(R.id.brand_name);
            start = (TextView) view.findViewById(R.id.car_brandstart);
            item_carBrand = (LinearLayout) view.findViewById(R.id.item_carBrand);
        }
    }

    public int getScrollPosition(String character) {
        if (characterList.contains(character)) {
            for (int i = 0; i < resultList.size(); i++) {
                if (resultList.get(i).getmName().equals(character)) {
                    return i;
                }
            }
        }

        return -1; // -1不会滑动
    }

    public  void  setData(List<CarPinpaiBean.DataBean> List){
        mPinpaiList.clear();
        mPinpaiList.addAll(List);
        notifyDataSetChanged();
    }
}
